package cn.sinata.wanglin.property.activity;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.property.view.WheelView;

public class ChooseTimeDialogActivity extends DialogActivity implements View.OnClickListener{

    private WheelView wv_day;
    private WheelView wv_hour;
    private WheelView wv_minute;
    private String mDay;
    private String mHour;
    private String mMinute;
    private int year;
    private String time;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_choose_time_dialog;
    }

    @Override
    protected void initView() {
        getWindow().setGravity(Gravity.BOTTOM);

        wv_day = bind(R.id.wv_day);
        wv_hour = bind(R.id.wv_hour);
        wv_minute = bind(R.id.wv_minute);

        final ArrayList<String> days=new ArrayList<>();
        final ArrayList<String> hours=new ArrayList<>();
        final ArrayList<String> minutes=new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        long nowTime = calendar.getTimeInMillis();
        String sNTime = TimeUtils.getMMDDWeek(nowTime);
        year = TimeUtils.getTimeYear(nowTime);
        days.add(sNTime);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        for (int i =0 ;i<6;i++) {
            day = day+1;
            calendar.set(Calendar.DAY_OF_MONTH,day);
            long time = calendar.getTimeInMillis();
            String sTime = TimeUtils.getMMDDWeek2(time);
            days.add(sTime);
        }
        for (int i=0;i<24;i++) {
            if (i < 10) {
                hours.add("0" + i + "时");
            } else {
                hours.add(i + "时");
            }
        }
        for (int i=0;i<6;i++) {
            if (i == 0) {
                minutes.add("00分");
            } else {
                minutes.add((i*10) + "分");
            }
        }

        wv_day.setItems(days);
        wv_hour.setItems(hours);
        wv_minute.setItems(minutes);

        wv_day.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                mDay = item.substring(0,5);
            }
        });

        wv_hour.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                mHour = item.substring(0,2);
            }
        });

        wv_minute.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                mMinute = item.substring(0,2);
            }
        });

        bind(R.id.tv_cancel).setOnClickListener(this);
        bind(R.id.tv_sure).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                onBackPressed();
                break;
            case R.id.tv_sure:
                mDay = wv_day.getSeletedItem().substring(0,5);
                mHour = wv_hour.getSeletedItem().substring(0,2);
                mMinute = wv_minute.getSeletedItem().substring(0,2);
                time = year+"-"+mDay+" "+mHour+":"+mMinute;
                Intent intent = new Intent();
                intent.putExtra("time",time);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }
}
